package cn.iocoder.yudao.module.shop.service.shopOpenTime;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.shop.controller.admin.shopOpenTime.vo.ShopOpenTimePageReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.shopOpenTime.vo.ShopOpenTimeSaveReqVO;
import cn.iocoder.yudao.module.shop.dal.dataobject.shopOpenTime.ShopOpenTimeDO;
import cn.iocoder.yudao.module.shop.dal.mysql.shopOpenTime.ShopOpenTimeMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.SHOP_OPEN_TIME_NOT_EXISTS;

/**
 * 商户营业时间 Service 实现类
 *
 * @author 吕小文
 */
@Service
@Validated
public class ShopOpenTimeServiceImpl implements ShopOpenTimeService {

    @Resource
    private ShopOpenTimeMapper shopOpenTimeMapper;

    @Override
    public Long createShopOpenTime(ShopOpenTimeSaveReqVO createReqVO) {
        // 插入
        ShopOpenTimeDO shopOpenTime = BeanUtils.toBean(createReqVO, ShopOpenTimeDO.class);
        shopOpenTimeMapper.insert(shopOpenTime);
        // 返回
        return shopOpenTime.getId();
    }

    @Override
    public void updateShopOpenTime(ShopOpenTimeSaveReqVO updateReqVO) {
        // 校验存在
        validateShopOpenTimeExists(updateReqVO.getId());
        // 更新
        ShopOpenTimeDO updateObj = BeanUtils.toBean(updateReqVO, ShopOpenTimeDO.class);
        shopOpenTimeMapper.updateById(updateObj);
    }

    @Override
    public void deleteShopOpenTime(Long id) {
        // 校验存在
        validateShopOpenTimeExists(id);
        // 删除
        shopOpenTimeMapper.deleteById(id);
    }

    private void validateShopOpenTimeExists(Long id) {
        if (shopOpenTimeMapper.selectById(id) == null) {
            throw exception(SHOP_OPEN_TIME_NOT_EXISTS);
        }
    }

    @Override
    public ShopOpenTimeDO getShopOpenTime(Long id) {
        return shopOpenTimeMapper.selectById(id);
    }

    @Override
    public PageResult<ShopOpenTimeDO> getShopOpenTimePage(ShopOpenTimePageReqVO pageReqVO) {
        return shopOpenTimeMapper.selectPage(pageReqVO);
    }

    @Override
    public void updateShopOpenTimeBatch(List<ShopOpenTimeSaveReqVO> list) {
        List<ShopOpenTimeDO> updateList = BeanUtils.toBean(list,ShopOpenTimeDO.class);
        updateList.forEach(item->{
            item.setDay(null);
        });
        shopOpenTimeMapper.updateBatch(updateList);
    }

}